package com.shelpe.services.sinbad.service.utils;

import java.util.Date;

import javax.persistence.AttributeConverter;

import org.apache.commons.lang.math.NumberUtils;

import com.shelpe.services.sinbad.entity.jpa.Adgroup.Status;
import com.shelpe.services.sinbad.entity.jpa.DriveType;
import com.shelpe.services.sinbad.entity.jpa.OnlineStatus;
import com.shelpe.services.sinbad.entity.jpa.Tactics;

public abstract class EntityUtils {

	
	public static class Day2IntConverter  implements AttributeConverter<Date, Integer>{

		@Override
		public Integer convertToDatabaseColumn(Date attribute) {
			return (int) (attribute.getTime() / 1000);
		}

		@Override
		public Date convertToEntityAttribute(Integer dbData) {
			long ts = dbData.intValue();
			return new Date(ts * 1000);
		}
		
	}
	
	public static class Day2StringConverter implements AttributeConverter<Date, String> {

		@Override
		public String convertToDatabaseColumn(Date attribute) {
			return attribute != null ? ((attribute.getTime() / 1000) + "") : "";
		}

		@Override
		public Date convertToEntityAttribute(String dbData) {
			if (dbData == null) {
				return null;
			}
			long ts = NumberUtils.toLong(dbData);
			if (ts <= 0) {
				return null;
			}
			return new Date(ts * 1000);
		}

	}
	
	public static class StatusConverter implements AttributeConverter<Status, Integer> {

		@Override
		public Integer convertToDatabaseColumn(Status attribute) {
			return attribute != null ? attribute.getValue() : Status.blank.getValue();
		}

		@Override
		public Status convertToEntityAttribute(Integer dbData) {
			return Status.from(dbData);
		}
	}
	
	public static class OnlineStatusConverter implements AttributeConverter<OnlineStatus, String> {

		@Override
		public String convertToDatabaseColumn(OnlineStatus attribute) {
			return attribute != null ? attribute.getValue() : OnlineStatus.online.getValue();
		}

		@Override
		public OnlineStatus convertToEntityAttribute(String dbData) {
			return OnlineStatus.from(dbData);
		}
	}
	
	public static class DriveTypeConverter implements AttributeConverter<DriveType, Integer> {

		@Override
		public Integer convertToDatabaseColumn(DriveType attribute) {
			return attribute != null ? attribute.getValue() : DriveType.manual.getValue();
		}

		@Override
		public DriveType convertToEntityAttribute(Integer dbData) {
			return DriveType.from(dbData);
		}
	}
	
	
	public static class Boolean2StringConverter implements AttributeConverter<Boolean, String> {

		@Override
		public String convertToDatabaseColumn(Boolean attribute) {
			return attribute.booleanValue() ? "true" : "false";
		}

		@Override
		public Boolean convertToEntityAttribute(String dbData) {
			return ("false".equals(dbData))?Boolean.FALSE:Boolean.TRUE;
		}
	}
	
	public static String fromSchedulePeriod(Tactics.SchedulePeriod attribute) {
		String ret = null;
		if (attribute != null) {
			if (attribute.getRanges() != null) {
				StringBuffer buffer = new StringBuffer();
				for (int i = 0; i < attribute.getRanges().size(); i++) {
					Tactics.Range r = attribute.getRanges().get(i);
					buffer.append(r.getFrom());
					buffer.append("-");
					buffer.append(r.getTo());
					buffer.append(",");
				}
				ret = buffer.substring(0, buffer.lastIndexOf(","));
			}
		}
		return ret;
	}

	public static Tactics.SchedulePeriod toSchedulePeriod(String dbData) {
		Tactics.SchedulePeriod s = new Tactics.SchedulePeriod();
		if (!(dbData.equals(""))) {
			// 00:00-08:00,08:00-18:00,18:00-24:00
			String[] rangeStr = dbData.split(",");
			for (String rr : rangeStr) {
				String from = rr.substring(0, rr.indexOf("-")); // from的形式:HH:mm
				String to = rr.substring(rr.indexOf("-") + 1); // to的形式:HH:mm
				Tactics.Range range = new Tactics.Range();
				range.setFrom(from);
				range.setTo(to);

				s.addRange(range);
			}
		}
		return s;
	}
}
